<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>教师端 - 签到管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #e6f2ff;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            background-image: url('background.jpg'); /* 添加背景图案 */
            background-size: cover;
        }
        .container {
            background-color: rgba(255, 255, 255, 0.9);
            padding: 30px;
            border-radius: 10px;
            text-align: center;
            margin-top: 30px;
            width: 90%;
            max-width: 800px;
            box-shadow: 0 0 20px rgba(0,0,0,0.2);
        }
        .teacher-div {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
        }
        h1 {
            color: #333;
        }
        form {
            margin: 20px 0;
        }
        label {
            font-size: 18px;
            color: #555;
        }
        input[type="text"] {
            padding: 10px;
            margin: 10px 0;
            width: 60%;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            padding: 10px 25px;
            background-color: #0066cc;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-top: 10px;
        }
        button:hover {
            background-color: #005bb5;
        }
        .qr-code {
            max-width: 50%;
        }
        img {
            max-width: 90%;
            height: auto;
        }
        h2 {
            color: #333;
        }
        ul {
            list-style-type: none;
            padding: 0;
        }
        li {
            background-color: #f2f2f2;
            margin: 5px 0;
            padding: 15px;
            border-radius: 5px;
            text-align: left;
        }
        .error {
            color: red;
        }
        .student-signed {
            width: 50%;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>教师端 - 签到管理</h1>
        
        {% if error %}
            <p class="error">{{ error }}</p>
        {% endif %}
        
        <form id="set-code-form" action="/teacher" method="POST">
            <label for="code">设置签到码：</label><br>
            <input type="text" id="code" name="code" required>
            <br>
            <button type="submit">生成二维码</button>
        </form>
        
        <div class="teacher-div">
            {% if session %}
            <div class="qr-code">
                <h2>签到二维码</h2>
                <img src="data:image/png;base64,{{ qr_code }}" alt="签到二维码">
                <p>扫码链接：<a href="{{ url_for('student_sign_in', code=session.code, _external=True) }}" target="_blank">{{ url_for('student_sign_in', code=session.code, _external=True) }}</a></p>
            </div>
            {% endif %}

            <div class="student-signed">
                <h2>已签到学生</h2>
                <button id="refresh-records">刷新记录</button>
                <ul id="records-list">
                    <!-- 签到记录将通过JavaScript填充 -->
                </ul>

                <br>
                <a href="/logout"><button>登出</button></a>
            </div>
        </div>
    </div>

    <script>
        // 刷新签到记录
        function refreshRecords() {
            fetch('/teacher/records')
            .then(response => response.json())
            .then(data => {
                var recordsList = document.getElementById('records-list');
                recordsList.innerHTML = '';
                if (data.records && data.records.length > 0) {
                    data.records.forEach(function(record) {
                        var listItem = document.createElement('li');
                        // 已经在后端调整过时间，这里直接显示
                        listItem.textContent = '姓名：' + record.student_id + '，签到时间：' + record.sign_in_time;
                        recordsList.appendChild(listItem);
                    });
                } else {
                    recordsList.innerHTML = '<li>暂无签到记录</li>';
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('获取签到记录失败：' + error);
            });
        }
    
        // 页面加载时自动刷新记录
        window.onload = function() {
            refreshRecords();
        };
    
        // 刷新记录按钮点击事件
        document.getElementById('refresh-records').addEventListener('click', function() {
            refreshRecords();
        });
    </script>
</body>
</html>